package com.test.mchelepesa.view_rice.info_input_rice;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import com.test.mchelepesa.R;
import com.test.mchelepesa.bean_rice.process_rice.ProcessData_rice;
import com.test.mchelepesa.util_rice.StringUtil_rice;
import com.test.mchelepesa.view_rice.RiceEditText;
import com.test.mchelepesa.view_rice.base_rice.BaseInfoInputViewManager_rice;

public class ContactViewManager_rice extends BaseInfoInputViewManager_rice {

    private TextView titleTv;
    private View clickView;
    private RiceEditText inputEt;

    private final String pageTag;

    private Runnable selectContactRunnable;

    private final boolean canSelect;

    @SuppressLint("ClickableViewAccessibility")
    public ContactViewManager_rice(View view, ProcessData_rice data, Runnable selectContactRunnable, String pageTag, boolean canSelect){
        super(view,data);
        this.pageTag = pageTag;
        this.selectContactRunnable = selectContactRunnable;
        this.canSelect = canSelect;


        titleTv = mView.findViewById(R.id.view_contact_input_tv_title);
        clickView = mView.findViewById(R.id.view_contact_input_view_click);
        inputEt = mView.findViewById(R.id.view_contact_input_et_input);

        setTitle(mData.getTitle());
        inputEt.setHint(StringUtil_rice.getSafeString(mData.getHint()));

        if (canSelect){
            inputEt.setEnabled(false);
            inputEt.setOnTouchListener((v, event) -> false);
            clickView.setVisibility(VISIBLE);
            clickView.setOnClickListener(v -> {
                if (selectContactRunnable!=null) selectContactRunnable.run();
            });
        }else {
            mView.findViewById(R.id.view_contact_input_icon).setVisibility(GONE);
            inputEt.setEnabled(true);
            inputEt.setBpType(pageTag+"_CONTACT_INPUT");
            clickView.setVisibility(GONE);
        }


    }

    @Override
    protected void init(){}

    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputEt.getText());
    }

    @Override
    public String getInput() {
        if (mData == null) return "";
        return mData.getValueOfTitle(StringUtil_rice.getSafeString(inputEt.getText().toString()));
    }

    public String getTextInput(){
        return StringUtil_rice.getSafeString(inputEt.getText().toString());
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

    public void setInput(String input){
        if (!TextUtils.isEmpty(input)){
            inputEt.setText(input);
        }
    }

}
